﻿#region Usings
using BK.BlusinessLogic;
using BK.Model;
using BK.Repositories.Abstract;
using BK.Repositories.Factory;

using Lib.Utils.WorkerUtils;
#endregion

namespace BK.Import
{
    public abstract class ExpensesBaseWorkerItem<TExpense, TExpensesFile, TExpensesFilesRepository, TExpensesRepository> :
        BaseWorkerItem<WorkerItemSettings, TExpense[]>
        where TExpense : class, IImportedEntity
        where TExpensesFilesRepository : IExpensesFilesRepository<TExpense, TExpensesFile>
        where TExpensesRepository : IExpensesRepository<TExpense>
    {
        #region Readonly & Static Fields
        private readonly BaseExpensesBl<TExpense, TExpensesFile> _expensesBl;
        #endregion

        #region C'tors
        protected ExpensesBaseWorkerItem() : this(
                RepositoryFactory.Get<IAccountsRepository>(),
                RepositoryFactory.Get<TExpensesFilesRepository>(), 
                RepositoryFactory.Get<TExpensesRepository>(),
                RepositoryFactory.Get<ICategoriesRepository>(),
                RepositoryFactory.Get<ICurrenciesRepository>(),
                RepositoryFactory.Get<IImportStatusesRepository>(),
                RepositoryFactory.Get<ITargetsRepository>(),
                RepositoryFactory.Get<IPaymentsRepository>())
        {
        }

        private ExpensesBaseWorkerItem(IAccountsRepository accountsRepository,
            IExpensesFilesRepository<TExpense, TExpensesFile> expensesFilesRepository,
            IExpensesRepository<TExpense> expensesRepository,
            ICategoriesRepository categoriesRepository,
            ICurrenciesRepository currenciesRepository,
            IImportStatusesRepository importStatusesRepository,
            ITargetsRepository targetsRepository,
            IPaymentsRepository paymentsRepository) : base(typeof(TExpense).Name + "_")
        {
            _expensesBl = CreateExpensesBl(accountsRepository, expensesFilesRepository, expensesRepository,
                categoriesRepository, currenciesRepository, importStatusesRepository, targetsRepository,
                paymentsRepository);
        }
        #endregion

        #region Instance Methods
        protected abstract BaseExpensesBl<TExpense, TExpensesFile> CreateExpensesBl(
            IAccountsRepository accountsRepository,
            IExpensesFilesRepository<TExpense, TExpensesFile> expensesFilesRepository,
            IExpensesRepository<TExpense> expensesRepository,
            ICategoriesRepository categoriesRepository,
            ICurrenciesRepository currenciesRepository,
            IImportStatusesRepository importStatusesRepository,
            ITargetsRepository targetsRepository,
            IPaymentsRepository paymentsRepository);

        protected override void DoExecute(TExpense[] objectToProcess)
        {
            foreach (TExpense expense in objectToProcess)
            {
                _expensesBl.ImportBestExense(expense);
            }
        }

        protected override TExpense[] GetFirstUnprocessedObject()
        {
            TExpense[] firstUnprocessedObject = _expensesBl.GetAllUnprocessed();
            if (firstUnprocessedObject.Length == 0)
            {
                firstUnprocessedObject = null;
            }

            return firstUnprocessedObject;
        }
        #endregion
    }
}